/**
 * Copyright (C) 2008 aileron.cc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package cc.aileron.commons.utility;

import org.apache.commons.vfs.FileContent;

import cc.aileron.commons.resource.Resource;
import cc.aileron.commons.resource.ResourceConvertUtils;
import cc.aileron.commons.resource.ResourceImpl;
import cc.aileron.commons.resource.ResourceLoaders;
import cc.aileron.commons.resource.ResourceNotFoundException;
import cc.aileron.commons.resource.ResourceNullObject;
import cc.aileron.commons.resource.ResourceLoaders.Type;


/**
 * classpath 上のリソースにアクセスする為のクラス
 * @author Aileron
 *
 */
public class ResourceUtils extends ResourceConvertUtils
{
	/**
	 * null
	 */
	private static Resource nullObject =
		new ResourceNullObject();
	
// file -----------------------------------------------------------------------

	/**
	 * リソースの取得
	 * @param path
	 * @return
	 */
	public static final Resource resourceNoneException(
		final FileContent content)
	{
		try
		{
			return resource(content);
		}
		catch(Exception e)
		{
			return nullObject;
		}
	}
	
	/**
	 * リソースの取得
	 * @param path
	 * @return
	 * @throws ResourceNotFoundException
	 */
	public static final Resource resource(
		final FileContent content)
	throws ResourceNotFoundException
	{
		return new ResourceImpl(content);
	}
	
// path -----------------------------------------------------------------------
	
	/**
	 * リソースの取得
	 * @param path
	 * @return
	 * @throws ResourceNotFoundException
	 */
	public static final Resource resource(
		final String path)
	throws ResourceNotFoundException
	{
		return ResourceLoaders.load(path);
	}
	
	/**
	 * リソースの取得
	 * @param path
	 * @return
	 */
	public static final Resource resourceNoneException(
		final String path)
	{
		try
		{
			return resource(path);
		}
		catch(ResourceNotFoundException e)
		{
			return nullObject;
		}
	}	

// ResourceLoader types
	
	/**
	 * リソースの取得
	 */
	public static final Resource resource(
	    final String path, final Type type)
	throws ResourceNotFoundException
	{
	    return ResourceLoaders.load(type, path);
	}
	
	/**
	 * vsf パス指定でのリソースの取得
	 * @param path
	 * @return
	 * @throws ResourceNotFoundException
	 */
	public static Resource vsfResource(
	        final String path) throws ResourceNotFoundException
	{
	    return ResourceLoaders.loadVsfResource(path);
	}
	
// utils ---------------------------------------------------------------------
	
	/**
	 * packageName to directoryName
	 * @param classObject
	 * @return
	 */
	public static final String packageNameToDirectoryName(
		final Package targetPackage)
	{
		return targetPackage.getName().replace('.', '/') + "/";
	}	
}